package edu.spsu.cs4253;

import java.util.*;
import java.io.*;
import java.net.*;

public class MasterRouter {
	
	private TreeMap<String, String[][]> ipTable;
	private String[][] ipAndPort;
	static String mRouterIP = "localhost";//Other Master Routers IP
	static int mRouterPort = 9999; //Other Master Router Port
	static String hostName = "localhost";
	static String rPort = "9999";
	static ServerSocket serverSocket = null; 
	static Socket clientSocket = null;
	static byte [] requestedFile = new byte[1024];
	
	public MasterRouter(){
        ipTable = new TreeMap<String, String [][]>();
        
        ipTable.put("Computer A", new String[][]{{"10.220.4.235"},{"5555"}});
        ipTable.put("Computer B", new String[][]{{"10.220.3.191"},{"8888"}});
        
        ipAndPort = new String[1][2];//First Column ipAddress; second column port#
    }
	
	public synchronized void add(String node, String ip, String port){
		ipAndPort[0][0]= ip;
		ipAndPort[0][1]= port;
		ipTable.put(node, ipAndPort);
	 } 
	
	public String[][] getIP(String node) {
	     return ipTable.get(node);
	    }
	
	public byte[] getFile(String ip, int port, String file) 
			                    throws UnknownHostException, IOException{
		DataOutputStream dos = null;
		DataInputStream dis = null;
		Socket sock = null;
		byte [] fileData = new byte[1024];
		
		 sock = new Socket(ip,port);
		 dos = new DataOutputStream(sock.getOutputStream());
		 dos.writeUTF(file);
		 dos.flush();
		
		 dis = new DataInputStream(sock.getInputStream());
		 int numOfBytes = dis.read(fileData);
		 
		 System.out.println("DEBUG: numOfBytes read in " + numOfBytes+ "byte(s).");
		dos.close();
		dis.close();
		
		return fileData;
		
	}
	
	public byte[] route(String node, String file) 
			             throws UnknownHostException, IOException{
		
		byte [] fileData = new byte[1024];
		
		if (ipTable.containsKey(node)){
			ipAndPort = ipTable.get(node);
			String ip = ipAndPort[0][0];
			int port = Integer.parseInt(ipAndPort[0][1]);
			
			fileData = getFile(ip, port,file);
		  }
		else 
			fileData = getFile(mRouterIP,mRouterPort,file);	
		 
		
		return fileData;
	}
	
	
	
	public static void main (String[] args) {
		
		MasterRouter router = new MasterRouter();
		int serverPort = Integer.parseInt(rPort);
		
		try {
			  serverSocket = new ServerSocket(serverPort);
			  
			  while(true){
			       clientSocket = serverSocket.accept();
			
			          if (clientSocket.isConnected()){
				           System.out.println("Connection was made.");
			               }
			      
			    DataInputStream readStream = new DataInputStream (clientSocket.getInputStream());
			     
			     Byte node = readStream.readByte();//reads in peer node
			     String file = readStream.readUTF();//reads in name of file requested
			     String peerNode = node.toString();
			    
			     
			     requestedFile = router.route(peerNode,file);
			     
			     DataOutputStream writeStream = new DataOutputStream(clientSocket.getOutputStream());
			     writeStream.write(requestedFile);
			    
			     serverSocket.close();
			     clientSocket.close();
			     readStream.close();
			  }
		    } 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
